/**
 * ReportGear(2011)
 */
package com.reportgear.report.model.cell.value;

import org.dom4j.Element;

import com.sot.core.util.NumberUtils;

/**
 * Js数据源对象
 * 
 * @version 1.0 2011-7-27
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class JsDataFieldObject extends AbstractCellObject {
	// 普通
	public static final int TYPE_NORMAL = 0;
	// 相片
	public static final int TYPE_PHOTO = 1;
	// 公式
	public static final int TYPE_FORMULA = 2;
	// 数据源Id
	private String dataSourceId;
	// 数据源标签
	private String dataSourceCaption;
	// 字段名
	private String field;
	// 字段标签
	private String fieldCaption;
	// 扩展
	private boolean expand = false;
	// 类型
	private int type = TYPE_NORMAL;
	// 公式
	private String formula;

	@Override
	public String toString() {
		if (this.type == TYPE_FORMULA) {
			return ":" + this.formula;
		} else {
			return this.dataSourceCaption + "." + this.fieldCaption;
		}
	}

	@Override
	public void read(Element parent) {
		this.dataSourceId = parent.elementTextTrim("datasource");
		this.dataSourceCaption = parent.elementTextTrim("dataSourceCaption");
		this.field = parent.elementTextTrim("field");
		this.fieldCaption = parent.elementTextTrim("fieldCaption");
		this.type = NumberUtils.convertToInteger(parent.elementTextTrim("type"), TYPE_NORMAL);
		this.expand = "1".equals(parent.elementTextTrim("expand"));
		this.formula = parent.elementTextTrim("formula");
	}

	@Override
	public void write(Element parent) {
		parent.addAttribute("class", ModelDesignableValueObjectClassManager.INSTANCE.getClassKey(this.getClass()));

		Element el = parent.addElement("datasource");
		el.addText(this.dataSourceId);

		el = parent.addElement("dataSourceCaption");
		el.addText(this.dataSourceCaption);

		el = parent.addElement("field");
		el.addText(this.field);

		el = parent.addElement("fieldCaption");
		el.addText(this.fieldCaption);

		el = parent.addElement("expand");
		el.addText(this.expand ? "1" : "0");

		el = parent.addElement("type");
		el.addText(String.valueOf(this.type));

		el = parent.addElement("formula");
		el.addText(this.formula);
	}

	/**
	 * 设置数据源Id
	 * 
	 * @param dataSourceId
	 *            数据源Id
	 */
	public void setDataSourceId(String dataSourceId) {
		this.dataSourceId = dataSourceId;
	}

	/**
	 * 获取数据源Id
	 * 
	 * @return 数据源Id
	 */
	public String getDataSourceId() {
		return dataSourceId;
	}

	/**
	 * 设置字段
	 * 
	 * @param field
	 *            字段
	 */
	public void setField(String field) {
		this.field = field;
	}

	/**
	 * 获取字段
	 * 
	 * @return 字段
	 */
	public String getField() {
		return field;
	}

	public void setDataSourceCaption(String dataSourceCaption) {
		this.dataSourceCaption = dataSourceCaption;
	}

	public String getDataSourceCaption() {
		return dataSourceCaption;
	}

	public void setFieldCaption(String fieldCaption) {
		this.fieldCaption = fieldCaption;
	}

	public String getFieldCaption() {
		return fieldCaption;
	}

	public void setExpand(boolean expand) {
		this.expand = expand;
	}

	public boolean isExpand() {
		return expand;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getType() {
		return type;
	}

	public void setFormula(String formula) {
		this.formula = formula;
	}

	public String getFormula() {
		return formula;
	}
}
